import {
  Component,
  Injector,
  ViewEncapsulation,
  ViewChild,
  AfterViewInit,
  ElementRef
} from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Http } from '@angular/http';
import {
  LocationServiceProxy,
  LocationDto
} from '@shared/service-proxies/service-proxies';
import { NotifyService } from '@abp/notify/notify.service';
import { AppConsts } from '@shared/AppConsts';
import { AppComponentBase } from '@shared/common/app-component-base';
import { FileDownloadService } from '@shared/utils/file-download.service';

import { CreateOrEditLocationModalComponent } from './create-or-edit-location-modal.component';

import { appModuleAnimation } from '@shared/animations/routerTransition';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Table } from 'primeng/table';

@Component({
  templateUrl: './location.component.html',
  styleUrls: ['./location.component.less'],
  animations: [appModuleAnimation()]
})
export class MaterialLocationComponent extends AppComponentBase
  implements AfterViewInit {
  @ViewChild('createOrEditLocationModal')
  createOrEditLocationModal: CreateOrEditLocationModalComponent;

  @ViewChild('dataTable') dataTable: Table;

  //Filters
  areas = Array<LocationDto>();
  shelfs = Array<LocationDto>();
  positions = Array<LocationDto>();
  id1 = 0;
  id2 = 0;
  selectedArea = new LocationDto();
  selectedShelf = new LocationDto();

  constructor(
    injector: Injector,
    private _http: Http,
    private _locationServiceProxy: LocationServiceProxy,
    private _notifyService: NotifyService,
    private _fileDownloadService: FileDownloadService
  ) {
    super(injector);
  }

  ngAfterViewInit(): void {
    this.getAreas();
  }

  reloadPage(type: number): void {
    if (type === 1) {
      this.getAreas();
    } else if (type === 2) {
      this.getShelfs();
    } else {
      this.getPositions();
    }
  }

  areaChange(event) {
    this.getShelfs();
  }

  shelfChange(event) {
    this.getPositions();
  }

  getAreas() {
    this._locationServiceProxy.getAreas().subscribe(result => {
      this.areas = result.items;
      if (result.items.length > 0) {
        this.selectedArea = result.items[0];
      } else {
        this.selectedArea = new LocationDto();
      }
      this.getShelfs();
    });
  }

  getShelfs() {
    this._locationServiceProxy
      .getShelvesByArea(this.selectedArea.id)
      .subscribe(result => {
        this.id1 = this.selectedArea.id;
        this.shelfs = result.items;
        if (result.items.length > 0) {
          this.selectedShelf = result.items[0];
        } else {
          this.selectedShelf = new LocationDto();
        }
        this.getPositions();
      });
  }

  getPositions() {
    this._locationServiceProxy
      .getPositionsByShelf(this.selectedShelf.id)
      .subscribe(result => {
        this.id2 = this.selectedShelf.id;
        this.positions = result.items;
      });
  }

  deleteLocation(lid: number, location: LocationDto): void {
    this.message.confirm('确认删除' + location.name + '吗', isConfirmed => {
      if (isConfirmed) {
        this._locationServiceProxy
          .deleteLocation(lid, location.id)
          .subscribe(() => {
            this.reloadPage(lid);
            this.notify.success(this.l('SuccessfullyDeleted'));
          });
      }
    });
  }
}
